package org.example.cet.demos.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.dto.SubDto;
import org.example.cet.demos.domain.dto.SubHomeworkDto;
import org.example.cet.demos.domain.dto.SubPaperDto;
import org.example.cet.demos.domain.form.SubmissionForm;
import org.example.cet.demos.domain.po.Submission;
import org.example.cet.demos.domain.query.SubmissionQuery;
import org.example.cet.demos.mapper.AnswerMapper;
import org.example.cet.demos.service.impl.AnswerServiceImpl;
import org.example.cet.demos.service.impl.SubmissionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/submission")
public class SubmissionController {
    @Autowired
    private SubmissionServiceImpl submissionService;
    @Autowired
    private converter converter;

    @Autowired
    private AnswerServiceImpl answerService;



    @Autowired
    private AnswerMapper answerMapper;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody SubmissionForm submissionForm) throws JsonProcessingException {
//        Submission submission=converter.ToSubmission(submissionForm);
//        String answerJson = objectMapper.writeValueAsString(submission.getAnswer());
//        submission.setAnswer(answer);
//        submissionService.save(submission);
//        return Result.success();

//        Submission submission = converter.ToSubmission(submissionForm);
//        String[] answerArray = submissionForm.getAnswer();
//        Long stuId = submission.getStuId();
//        int type=submission.getType();
//        int questionId = submission.getQuestionId();
//        Map<String, Object> resultMap = answerService.calculation(stuId, questionId, answerArray);
//        String[] result = (String[]) resultMap.get("result");
//        double scorePart = (double) resultMap.get("score");
//        submission.setType(type);
//        submission.setAnswer(answerArray);
//        submission.setResult(result);
//        submissionService.save(submission);
        Submission submission =converter.ToSubmission(submissionForm);
        String title=submissionForm.getTitle();
        String[] answerArray = submissionForm.getAnswer();
        String stuId = submission.getStuId();
        int questionId = submission.getQuestionId();
        int type = submission.getType();
        Long homeworkId = submission.getHomeworkId();

        String bankId=submission.getBankId();
        // 调用 AnswerService 进行分数计算和保存操作
        Map<String, Object> resultMap=answerService.calculation(submission, stuId, questionId, answerArray);

        answerService.handleSubmission(title,bankId, stuId, questionId, answerArray,homeworkId,type);

//        answerService.createScoreBankRecord(bankId,stuId, type);

//        answerService.createScoreDetailRecord(questionId,qType,scorePart,bankId);

//        answerService.totalScore(bankId);


//        scoreDetailService.insert(questionId,qType,score);
//        scoreBankService.insert(stuId,type,score);

        return Result.success(resultMap); // 返回分数信息给前端

}

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        submissionService.removeById(id);
        return Result.success();
    }

//    @PutMapping("/update/{id}")
//    public Result update(@PathVariable Long id,@RequestBody String answer) {
////        @Valid @RequestBody SubmissionForm submissionForm
////        Submission submission=converter.ToSubmission(submissionForm);
////        submission.setId(id);
//        Submission submission = submissionService.getById(id);
//        if (submission != null) {
//            Long stuId = submission.getStuId();
//            int questionId = submission.getQuestionId();
//            String[] answerArray = {answer};
//            Map<String, Object> resultMap = answerService.calculation(stuId, questionId, answerArray);
//            String result = (String) resultMap.get("result");
//            submission.setAnswer(answer);
//            submission.setResult(result);
//            submissionService.updateById(submission);
//        }
////        submissionService.update(id,answer);
//        return Result.success();
//    }

    @GetMapping("/getPage")
    public Result getPage(SubmissionQuery submissionQuery) {
        PageBean pb=submissionService.getPage(submissionQuery);
        return Result.success(pb);
    }

    @GetMapping("/getList/{type}")
    public Result list(@PathVariable Integer type){
        return Result.success(submissionService.getList(type));
    }

    @GetMapping("/findId/{id}")
    public Result findId(@PathVariable Long id){
        List<SubDto> zx=submissionService.findId(id);
        return Result.success(zx);
    }

    @GetMapping("/findHomework/{homeworkId}")
    public Result findHomework(@PathVariable Long homeworkId){
        List<SubHomeworkDto> zy=submissionService.findHomework(homeworkId);
        return Result.success(zy);
    }

    @GetMapping("/findPaper/{bankId}")
    public Result findPaper(@PathVariable String bankId){
        List<SubPaperDto> sj=submissionService.findPaper(bankId);
        return Result.success(sj);
    }


}
